var ul = document.getElementById("gun"), 
olList = document.getElementById("ol").children
wraperS = document.getElementById("wraperS"),scrollBtnR = document.getElementById("scrollBtnR"),
scrollBtnL= document.getElementById("scrollBtnL");
var position = 0, back = -ul.offsetWidth - 840;
ul.innerHTML = ul.children[4].outerHTML + ul.innerHTML + ul.children[0].outerHTML,
index = 0, lock = false;
ul.style.left = "-840px";

function change() {
    for (var i = 0 ; i < olList.length ; i++) {
        olList[i].className = "";
    }
    olList[index].className = "current"
}

function moveDistance (ele,distance,time) {
    var maxCount = time / 100, count = 0, step = distance / maxCount, 
    start = parseInt(window.getComputedStyle(ele)["left"]);
    var timer;
    timer = setInterval(function () {
        start -= step;
        count++;
        if (count >= maxCount) {
            switch (index) {
                case 0:
                    end = -5040;
                    break;
                default:
                    end = -(index + 1) * 840
            }
            start = end;
            clearInterval(timer);
        }
        if (start < back) {
            start = -840;
        } else if (start > 0) {
            start = back;
        }
        ele.style.left = start +"px";
    },100)
}
function scrollR() {
    if (lock) {
        return;
    }
    moveDistance(ul,840,2000);
    index++;
    if (index > olList.length - 1) {
        index = 0;
    }
    change();
    lock = true;
    setTimeout(function () {
        lock = false;
    },2000)
}

scrollBtnR.onclick = scrollR;

scrollBtnL.onclick = function () {
    if (lock) {
        return;
    }
    moveDistance(ul,-840,2000);
    index--;
    if (index < 0) {
        index = olList.length - 1;
    }
    change();
    lock = true;
    setTimeout(function () {
        lock = false;
    },3000)
}

var timer1;
timer1 = setInterval(scrollR, 3000);

wraperS.onmouseover = function () {
    clearInterval(timer1);
};

wraperS.onmouseout = function () {
    clearInterval(timer1);
    timer1 = setInterval(scrollR, 3000);
}